<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

class CprojectModelSections extends JModelList {

    var $_db = null;

    protected function populateState($ordering = null, $direction = null) {

        $app = JFactory::getApplication('administrator');

        //Load State 

        $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
        $this->setState('filter.search', $search);

        $published = $this->getUserStateFromRequest($this->context . '.filter.state', 'filter_published', '', 'string');
        $this->setState('filter.state', $published);

        parent::populateState('a.name', 'asc');
    }

    protected function getStoreId($id = '') {
        // Compile the store id.
        $id.= ':' . $this->getState('filter.search');
        $id.= ':' . $this->getState('filter.state');
        return parent::getStoreId($id);
    }

    function getListQuery() {
        $where = array();

        $where = $this->_buildContentWhere();
        $this->_db = JFactory::getDbo();
        $query = $this->_db->getQuery(true);
        $query = CprojectDBHelper::getCprojectSection($sql=true, $where);
        return $query;
    }

    function _buildContentWhere() {
        $where = array();
        $this->_db = JFactory::getDbo();

        //Filter by published state 
        $published = $this->getState('filter.state');
        if (is_numeric($published)) {
            $where[] = 'a.published = ' . (int) $published;
        } else if ($published == '') {
            $where[] = 'a.published IN (0, 1)';
        }

        //Tim kiem theo ten
        $search = $this->getState('filter.search');

        if (!empty($search)) {

            $search = $this->_db->Quote('%' . $this->_db->getEscaped($search, true) . '%');
            $where[] = '(a.name LIKE ' . $search . ')';
        }
        return $where;
    }

}

?>
